<?php        
    class UserModel extends Model {
        public static $classDesc = "User";
        
        public static $keyDesc = array(
            "id"            => "ID",
            "hashid"        => "Hashid",
            "email"         => "Email",
            "password"      => "Password",
            "firstname"     => "Firstname",
            "Lastname"      => "Lastname",
            "timezoneid"    => "Timezone",
            "default_unit"  => "Unit",
            "timeout_min"   => "Timeout",
            "updated"       => "Updated",
            "rowstamp"      => "Created"
        );
        
        public function __construct($id = null) {
            $this->_childClass["user_workout_share"] = "UserWorkoutShareModel";
            
            $this->_childs["user_workout_share"] = null;
            
            $this->_foreignIdCol    = "userid";
            $this->_table           = "user";
            $this->_userIdCol       = "id";
            
            parent::__construct($id);
        }
        
        public function __set($key,$value) {
            switch ($key) {
                case "email": # Sähköpostiosoitteen asetus                    
                    $email = $value;
                    if (is_array($value) && array_key_exists($key,$value)) {
                        $email = $value[$key];
                    }
                    
                    if (is_string($email) && $email && $email != $this->email) {
                        # Tarkastetaan löytyykö sähköpostiosoite jo tietokannasta
                        $idCol = self::IDCOL;
                        $query = DB::preparedQuery("SELECT {$idCol} FROM {$this->_table} WHERE email=:email LIMIT 1",array(
                            "email" => $email
                        ));
                        if (DB::numRows($query) == 1) {
                            DB::freeResult($query);
                            
                            throw new ValidationException(
                                "Email already exists",
                                1000,
                                get_class($this),
                                "email",
                                $email                        
                            );
                        }
                    }
                    
                    parent::__set($key,$value);
                    break;
                
                default:
                    parent::__set($key,$value);
            }
        }
        
        /**
         * Funktio asettaa käyttäjätunnuksen salasanan.
         *
         * @param   string  $password   Salasana
         * @param   string  $password2  Salasana uudestaan (vahvistus)
         */
        public function setPassword($password,$password2) {
            if ((!is_string($password) && !is_numeric($password))
                || mb_strlen($password) < 6) {
                throw new ValidationException(
                    "Password must have at least six characters",
                    1001,
                    get_class($this),
                    "password",
                    $password
                );
            }
            
            if ($password != $password2) {
                throw new ValidationException(
                    "Password do not match the confirm password",
                    1002,
                    get_class($this),
                    "password",
                    $password
                );
            }
            
            $this->password = password_hash($password,PASSWORD_DEFAULT);
        }
    }